package com.vn.ducktohome.screen.play;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.vn.ducktohome.constant.Constant;
import com.vn.ducktohome.enums.ScreenEnum;
import com.vn.ducktohome.gamelogic.GameControl;

public class LeftPanel extends Group {

	private int iTime, iLevel;
	private Image imgKhung1, imgKhung2, imgKhung3, imgKhung4;
	private Label lTime, lLevel, lLives;
	private Label lTime2, lLevel2, lLives2;
	private TextButton tbBack;
	private float deltaTime = 0;

	private int x1 = 30, x2 = 70, x3 = 80, y1 =310, y2, y3, y4, w = 120, h = 30;

	public LeftPanel() {
		super();
		initValues();
		init();
	}

	private void initValues() {
		iTime = 59;
		iLevel = Constant.ACTIVE_LEVEL;
	}

	private void init() {
		y2 = y1 - 65;
		y3 = y2 - 65;
		y4 = y3 - 65;
		
		//init title
		Image imgTitle = new Image(GameControl.getAtlas().findRegion("play/title"));
		imgTitle.setX(15);
		imgTitle.setY(350);
		this.addActor(imgTitle);
		
		NinePatch npKhung = new NinePatch(GameControl.getAtlas().findRegion("play/khungNP"), 3, 3, 3, 3);
		// init back
		imgKhung1 = new Image(npKhung);
		initPosition(imgKhung1, x1, y1, w, h);
		this.addActor(imgKhung1);

		imgKhung2 = new Image(npKhung);
		initPosition(imgKhung2, x1, y2, w, h);
		this.addActor(imgKhung2);

		imgKhung3 = new Image(npKhung);
		initPosition(imgKhung3, x1, y3, w, h);
		this.addActor(imgKhung3);

		// init label
		lTime = new Label("Time", GameControl.getMySkin());
		lTime.setX(x2);
		lTime.setY(y1 + h);
		this.addActor(lTime);

		lLevel = new Label("Level", GameControl.getMySkin());
		lLevel.setX(x2);
		lLevel.setY(y2 + h);
		this.addActor(lLevel);

		lLives = new Label("Lives", GameControl.getMySkin());
		lLives.setX(x2);
		lLives.setY(y3 + h);
		this.addActor(lLives);

		// init label on back
		lTime2 = new Label(String.valueOf(iTime), GameControl.getMySkin());
		lTime2.setX(x3);
		lTime2.setY(y1 + 5);
		lTime2.setColor(Color.RED);
		this.addActor(lTime2);

		lLevel2 = new Label(String.valueOf(iLevel), GameControl.getMySkin());
		lLevel2.setX(x3);
		lLevel2.setY(y2 + 5);
		lLevel2.setColor(Color.RED);
		this.addActor(lLevel2);

		lLives2 = new Label(String.valueOf(Constant.LIVES), GameControl.getMySkin());
		lLives2.setX(x3);
		lLives2.setY(y3 + 5);
		lLives2.setColor(Color.RED);
		this.addActor(lLives2);
		
		//initBack button
		tbBack = new TextButton("back", GameControl.getMySkin(), "button-red");
		tbBack.setX(x1);
		tbBack.setY(y4 + 5);
		tbBack.setWidth(w);
		tbBack.setHeight(35);
		this.addActor(tbBack);
		initClick();
		
		//initTip
		NinePatch npTip = new NinePatch(GameControl.getAtlas().findRegion("play/tipNP"), 4, 4, 4, 4);
		Image imgTip = new Image(npTip);
		imgTip.setX(x1);
		imgTip.setY(5);
		imgTip.setWidth(220);
		imgTip.setHeight(40);
		this.addActor(imgTip);
		
		Label lTip = new Label("Touch lake to go home", GameControl.getMySkin());
		lTip.setX(imgTip.getX() + 20);
		lTip.setY(12);
		lTip.setColor(Color.ORANGE);
		this.addActor(lTip);
	}

	private void initPosition(Image img, int x, int y, int w, int h) {
		img.setX(x);
		img.setY(y);
		img.setWidth(w);
		img.setHeight(h);
	}
	
	private void initClick(){
		tbBack.addListener(new ClickListener(){
			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				GameControl.getScreenManager().createScreen(ScreenEnum.SELECT_LEVEL_SCREEN);
				GameControl.getScreenManager().getScreen().show();
			}
		});
	}

	@Override
	public void act(float delta) {
		super.act(delta);
		deltaTime += Gdx.graphics.getDeltaTime();
		if ( deltaTime > 1 ) {
			deltaTime = 0;
			iTime -= Gdx.graphics.getDeltaTime();
			lTime2.setText(String.valueOf(iTime));
			if (iTime <= 0){
				GameControl.getScreenManager().createScreen(ScreenEnum.SELECT_LEVEL_SCREEN);
				GameControl.getScreenManager().getScreen().show();
			}
		}
	}
}
